﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Build.Evaluation;
using ProjManager.Helper;
using ProjManager.Model;

namespace ProjManager
{
	class Program
	{
		public static void Main(string[] args)
		{
			string slnFileName = "D:/workspace_jenkins/AuthTest/Amass.Auth.sln";
			string fileName=string.Empty;
			string fileContent=string.Empty;
			GetSolutionFile(slnFileName,ref fileName,ref fileContent);
			List<ProjectPropertyModel> projectPropertyModels = GetProjList(fileName,fileContent);
			
			for (int i = 0; i < projectPropertyModels.Count; i++) {
				ProjectCollection pc = new ProjectCollection();
				Project _Project = pc.LoadProject(projectPropertyModels[i].ProjectAbsolutePath);
				Console.WriteLine("程序集文件夹				{0}",_Project.DirectoryPath);
				Console.WriteLine("获取程序集名称			{0}",_Project.GetPropertyValue("AssemblyName"));
				Console.WriteLine("获取编译后文件夹			{0}",_Project.GetPropertyValue("TargetDir"));
				Console.WriteLine("获取编译后路径			{0}",_Project.GetPropertyValue("TargetPath"));
				Console.WriteLine("获取所有引用项			{0}",_Project.AllEvaluatedItems);
				//Console.WriteLine("获取所有引用项			{0}",_Project.AllEvaluatedItems.Where(e => e.ItemType == "Reference"));
				//Console.WriteLine("获取所有编译项			{0}",_Project.AllEvaluatedItems.Where(e => e.ItemType == "Compile"));
				//Console.WriteLine("获取所有页面				{0}",_Project.AllEvaluatedItems.Where(e => e.ItemType == "Page"));
				//Console.WriteLine("获取其他项				{0}",_Project.AllEvaluatedItems.Where(e => e.ItemType == "None"));
				//Console.WriteLine();
			}
			Console.ReadKey(true);
		}
		
		private static void GetSolutionFile(string slnFileName,ref string slnFolderPath,ref string fileContent)
		{
			slnFolderPath = System.IO.Path.GetDirectoryName(slnFileName);
			fileContent = ReaderFile(slnFileName);
		}
		
		private static List<ProjectPropertyModel> GetProjList(string slnFolderPath,string fileContent)
		{
			var projectRegexExp = 
				string.Format("Project\\(\"{{{0}}}\"\\)\\s*=\\s*\"{1}\"\\s*,\\s*\"{2}\"\\s*,\\s*\"{{{3}}}\"",
				              RegexExpressionConst.GuidExp,
				              RegexExpressionConst.ProjectExt,
				              RegexExpressionConst.RelativePathExt,
				              RegexExpressionConst.GuidExp);
			
			var projectPropertyModels = new List<ProjectPropertyModel>(); 
			var projectReg = new Regex(projectRegexExp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
			var projectContexts = projectReg.Matches(fileContent);
			foreach (var projectContext in projectContexts)
			{
			    var projectPropertyModel = new ProjectPropertyModel();
			    var projectContextArr = projectContext.ToString().Split(new string[]{"="}, StringSplitOptions.RemoveEmptyEntries);
			    projectPropertyModel.ProjectTypeID = new Regex(RegexExpressionConst.GuidExp).Match(projectContextArr[0]).ToString().Trim();
			    var projectValueArr = projectContextArr[1].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
			    projectPropertyModel.ProjectName = projectValueArr[0].Replace("\"", "").Trim();
			    projectPropertyModel.ProjectRelativePath = projectValueArr[1].Replace("\"", "").Trim();
			    projectPropertyModel.ProjectID = projectValueArr[2].Replace("\"{", "").Replace("}\"", "").Trim();
			    projectPropertyModel.ProjectAbsolutePath = Path.Combine(slnFolderPath, projectPropertyModel.ProjectRelativePath);
			    projectPropertyModels.Add(projectPropertyModel);
			}	

			return projectPropertyModels;
		}
		
		public static string ReaderFile(string path)
        {
            string fileData = string.Empty;
            try
            {       
                StreamReader reader = new StreamReader(path, Encoding.Default);
                fileData = reader.ReadToEnd();
                reader.Close();
            }
            catch (Exception ex)
            {
				throw;  
            }      
            return fileData;
        }
		
	}
}